package com.smvc.convertors;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import com.smvc.entities.User;
/**
 * 类型转换器，需要注入到IOC容器中
 * @author liuxing
 *
 */
@Component
public class UserConvertor implements Converter<String, User> {

	@Override
	public User convert(String source) {
		if (source != null) {
			String[] vals = source.split("-");
			if (vals != null && vals.length == 3) {
				int id = Integer.parseInt(vals[0]) ;
				String name = vals[1];
				int  age = Integer.parseInt(vals[2]);
				return new User(id, name, age);
			}
		}
		return null;
	}

}
